<template>
  <div id="infor">  

    <div  style="display:flex;align-items: center;">
            <img class="top_img" src="@/assets/images/a8.png" style="width:35px;height:35px;margin-right:10px;"/>
            <div style="line-height: 40px;font-weight:bold:">介绍管理</div>
     </div> 

  <el-row>
        <el-col :span="24" style="text-align: right;">         
            <el-button type="primary" class="custom_color_button1" style="margin-bottom:10px;"  @click="add_infor_but">
              <img class="custom_color_button1_img" src="@/assets/images/but_bg2.png" />添加信息
            </el-button>         
        </el-col>
  </el-row>


   <!-- 渲染数据list -->
   <infor-list :list="list" @up_list_fun="list_fun"> </infor-list>

  
   <el-dialog title="添加信息" :visible.sync="add_infor_mtk"  width="800px" >
          <el-form ref="form" :model="formlist"  label-width="100px" style="width:100%;">
                <el-form-item label="标题">
                    <el-input v-model="formlist.title" placeholder="必填"></el-input>
                </el-form-item> 

             <!-- 富文本选取图片 -->
                <el-form-item label="详情" style="">
                    <div style="">
                        <quill-editor class="editor"
                            v-model="formlist.content" 
                            ref="myQuillEditor" 
                            :options="editorOption" 
                            @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
                            @change="onEditorChange($event)" style="height:300px;margin-bottom:100px;">
                        </quill-editor>
                    </div>
                </el-form-item>
            <!-- 富文本选取图片 -->
            <el-form-item label="" v-show="false" >  
                <el-upload                                                
                    action="uploadUrl"  
                    :show-file-list="false"   
                    :before-upload="beforeupload"
                    :http-request="ImgUploadSectionFile">
                        <el-button slot="trigger" size="small" type="primary" id="selectImg">选取文件</el-button>
                </el-upload>
            </el-form-item>

              <el-form-item label="外侧封面">
                      <el-upload 
                            action="uploadUrl"  
                            :show-file-list="false"   
                            :before-upload="beforeupload1"
                            :http-request="ImgUploadSectionFile">
                            <div class="avatar-uploader">
                                <img v-if="pic_form1.head_img"  :src="pic_form1.head_img" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </div>
                      </el-upload>
                </el-form-item>

                 <el-form-item label="视频封面">
                      <el-upload 
                            action="uploadUrl"  
                            :show-file-list="false"   
                            :before-upload="beforeupload3"
                            :http-request="ImgUploadSectionFile">
                            <div class="avatar-uploader">
                                <img v-if="pic_form3.head_img"  :src="pic_form3.head_img" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </div>
                      </el-upload>
                </el-form-item>


                 <el-form-item label="视频">
                       <el-upload 
                            action="uploadUrl"  
                            :show-file-list="false"   
                            :before-upload="beforeupload2"
                            :http-request="ImgUploadSectionFile"
                            >
                             
                            <div class="avatar-uploader"> 
                                <video  v-if="video_form.video_url"  :src="urlimg  + video_form.upvideofile" controls="controls" style="width:100%;"></video>
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </div>

                           
                       </el-upload>
                   </el-form-item>

            

         </el-form>
          <div style="text-align: center;width:80%;margin:auto;">
                  <el-button type="primary" style="width:70%;" class="custom_color_button" @click="add_infor_but_qr">确认</el-button>
          </div>

   </el-dialog>  
      
   <div style="margin-top:20px;text-align: center;">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :page-size="10"  
        layout="prev, pager, next, jumper"
        :total="count">
      </el-pagination>
    </div>

    

      
  </div>
</template>

<script>
import toolbarOptions from '@/assets/js/quillConfig.js'
import InforList from '@/view/Infor/InforList'
export default {
  name: 'infor',
  data () {
    return {
        list:[],
        page:1,
        count:0,
        add_infor_mtk:false,
        formlist:{
            content:'',
            upfile:'',
            title:''
        },
        pic_form1:{
           head_img:'',
           upfile:'',
        },
        pic_form3:{
           head_img:'',
           upfile:'',
        },
        video_form:{
          video_url:'',
          upvideofile:''
        },
        editorOption: {//富文本
          modules: {
            toolbar: {
              container: toolbarOptions,  // 工具栏
              handlers: {
                'image': function (value) {
                  if (value) {
                    // alert('自定义图片')
                    document.getElementById("selectImg").click();    //调用选择图片
                    
                  } else {
                   // this.quill.format('image', false);
                  }
                }
              }
            }
           }       
        },
    }
  },
 components:{
    InforList
  },
  created(){
    this.list_fun()
  },
  mounted (){
    
  },
  methods: {
        list_fun(){
          this.post("/future/listOfData", {
              page:this.page, 
	        }).then(res => {
            this.count = res.result.count
            this.list = res.result.list
            //console.log(res)
         })
      },
     add_infor_but(){
         this.add_infor_mtk = true
         this.formlist = {
            content:'',
            upfile:'',
            title:''
         }
         this.pic_form1={
           head_img:'',
           upfile:'',
         }
         this.pic_form3={
           head_img:'',
           upfile:'',
         }
         this.video_form={
          video_url:'',
          upvideofile:''
         }
     },
     add_infor_but_qr(){
         this.post("/future/create", {
                title:this.formlist.title,
                content:this.formlist.content,
                cover:this.pic_form1.upfile,
                video_cover:this.pic_form3.upfile,
                video_link:this.video_form.upvideofile
            }).then(res => {

                if(res.error_code == 0){
                    this.open1(res.result,'success')  //提示框
                    this.list_fun()
                    this.add_infor_mtk = false
                }else {
                this.open1(res.message,'warning')
            }

        })
     },
     ImgUploadSectionFile(param){//图片上传    
     },
      beforeremove(){
          return false
      },
      onEditorBlur(){ //富文本
     },
     onEditorFocus(){//富文本获得焦点事件
     },
     onEditorChange(){//富文本内容改变事件
      //console.log(this.xg_school.content)
     },
     beforeupload(file){   //重新写入上穿 需要点击确认上传与表单一起提交
           if(this.upimglx(file) == false){  //判断是否为fales
              this.upimglxtitle()  //执行弹出错误             
              return false
            }            
            this.upimgpost("/image/uploadMultiple",file).then(res => {
            this.formlist.upfile = res.result.url   
            this.formlist.content = this.formlist.content +'<img src="'+  this.urlimg + this.formlist.upfile +'"/>'

           })

          return false;
      },
      beforeupload1(file){
        if(this.upimglx(file) == false){  //判断是否为fales
              this.upimglxtitle()  //执行弹出错误             
              return false
            }  
            var windowURL = window.URL || window.webkitURL;               
            this.pic_form1.head_img=windowURL.createObjectURL(file);                
            
             this.upimgpost("/image/uploadMultiple",file).then(res => {
               console.log(res.result.url )
               this.pic_form1.upfile = res.result.url    
            })

            return false;

     },
     beforeupload2(file){   //重新写入上穿 需要点击确认上传与表单一起提交
           if(this.upvideolx(file) == false){  //判断是否为fales
              this.upvideolxtitle()  //执行弹出错误             
              return false
            }  

            const loading = this.$loading({   //视频上传遮罩层
                lock: true,
                text: '视频上传中',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });

            var windowURL = window.URL || window.webkitURL;               
            this.video_form.video_url=windowURL.createObjectURL(file);             
            //console.log(file)   
            
            this.upimgpost("/image/uploadMultiple",file).then(res => {
            this.video_form.upvideofile = res.result.url 
             loading.close();  //关闭视频上传遮罩层
            console.log(res)

           })

            return false;
      },
      beforeupload3(file){
        if(this.upimglx(file) == false){  //判断是否为fales
              this.upimglxtitle()  //执行弹出错误             
              return false
            }  
            var windowURL = window.URL || window.webkitURL;               
            this.pic_form3.head_img=windowURL.createObjectURL(file);                
            
             this.upimgpost("/image/uploadMultiple",file).then(res => {
               console.log(res.result.url )
               this.pic_form3.upfile = res.result.url    
            })

            return false;

     },
     handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.page = val
        this.list_fun()
        console.log(`当前页: ${val}`);
      }
  }
}
</script>


<style scoped>
.avatar-uploader  {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
  .avatar {
    width: 150px;
    height: 150px;
    display: block;
  }
</style>
